Skip to content

Comments

London | 25-SDC-Nov | Aida Eslamimoghadam | Sprint 4 | Implement Shell tools in python #270

Open
aydaeslami wants to merge 9 commits intoCodeYourFuture:mainfrom
aydaeslami:Python-Shell-tools
Open

London | 25-SDC-Nov | Aida Eslamimoghadam | Sprint 4 | Implement Shell tools in python #270
aydaeslami wants to merge 9 commits intoCodeYourFuture:mainfrom
aydaeslami:Python-Shell-tools

Conversation

@aydaeslami
Copy link

@aydaeslami aydaeslami commented Dec 1, 2025

  • I have titled my PR with Region | Cohort | FirstName LastName | Sprint | Assignment Title
  • My changes meet the requirements of the task
  • I have tested my changes
  • My changes follow the style guide

This PR includes my solutions to simulate several shell tools in Python.
I used the argparse module to re-implement them.

@aydaeslami aydaeslami added 📅 Sprint 4 Assigned during Sprint 4 of this module Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Dec 1, 2025
Copy link

@DaryaShirokova DaryaShirokova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great progress on this! Left a few comments here

description="Simple cat clone with -n and -b options",
)

counterNumber = 1

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In python the usual code style is to use snake_case.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing that out. I’ll update the variable.

print(counterNumber, arrText[i])
counterNumber += 1
else:
print(content) No newline at end of file

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please wrap you logic into a method and run it using if __name__ == "__main__": approach? Ideally the program should not contain global vars, and having methods improves readability.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion. I’ve moved the logic into functions, added a main() entry point using if name == "main": and removed global variables to improve readability.

)

counterNumber = 1
parser.add_argument("-n", action="store_true", help="number all lines")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cat -n sample-files/1.txt, cat.py sample-files/ *.txt (including -n option) and cat -b sample-files/3.txt all show slightly different result from yours.

for file_path in args.path:
with open(file_path, "r") as f:
content = f.read()
arrText = content.split("\n")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a method in python to read file into an array of lines directly.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the suggestion. I’ll use the built-in method to read the file into a list of lines directly.

arrText = content.split("\n")

if args.b:
for i in range(len(arrText )):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can potentially use enumerate here.

parser.add_argument("path", nargs="?", default=".", help="The directory to list")
args = parser.parse_args()


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, formatting could be improved with ruff here or other formatters :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the suggestion @DaryaShirokova.
I’ve formatted the code using ruff to improve consistency and readability.

)

parser.add_argument("-a", action="store_true", help="include hidden files")
parser.add_argument("-1", dest="one" ,action="store_true", help="use a long listing format")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by long listing format here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many thanks for your time and help.
I’ll update the help text to reflect that.

import argparse
import os

def show_unhidden_files(listDir):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can these two functions be merged into one using extra method param?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion.
I merged them into a single function with an extra parameter.

parser.add_argument("path", nargs="+", default=".", help="The file to count")
args = parser.parse_args()

if not args.l and not args.w and not args.c:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you rework this code to have less duplication, by using methods?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your feedback, I’ve refactored the code to reduce duplication.

@@ -0,0 +1,66 @@
import argparse
parser = argparse.ArgumentParser(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please enasure your output is the same as the one suggested for examples in the readme (it is ok if whitespaces are different in your output):

It must act the same as `wc` would, if run from the directory containing this README.md file, for the following command lines:

* `wc sample-files/*`
* `wc -l sample-files/3.txt`
* `wc -w sample-files/3.txt`
* `wc -c sample-files/3.txt`
* `wc -l sample-files/*`

@DaryaShirokova DaryaShirokova added Reviewed Volunteer to add when completing a review with trainee action still to take. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Jan 2, 2026
@aydaeslami aydaeslami added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Feb 11, 2026
Comment on lines 11 to 26
if args.b:
for line in lines:
if line.strip() != "":
print(counter_number, line, end="")
counter_number += 1
else:
print(line, end="")

elif args.n:
for line in lines:
print(counter_number, line, end="")
counter_number += 1

else:
for line in lines:
print(line, end="")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works, but often when doing this kind of "there are a few ways we may want to change things, but we're doing the same thing at its core", we try to structure code to emphasise what's the same/different, so something like:

for line in lines:
    prefix = ""
    if args.n or (args.b and line.strip() != ""):
        prefix = counter_number
        counter_number += 1
    print(f"{prefix} {line}")

This makes clear that regardless of flags, we're doing something for each line, we're printing the line, and we may print something different at the start.

It particularly helps when we end up with lots of different combinations, to think how each affects what we're doing, rather than having to handle them all separately.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @illicitonion. that’s a really helpful suggestion. I’ve refactored the code to emphasise the shared logic and handle the differences through a prefix, as you described.

Comment on lines 7 to 11
if show_hidden:
print(file)
else:
if not file.startswith("."):
print(file)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same idea here:

if show_hidden or not file.startswith("."):
    print(file)

This way if we change how we print things, we only need to change it in once place, not two.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the suggestion @illicitonion. That was a really valuable point.
I have refactored the code to centralise the printing logic.
Many thanks.

args = parser.parse_args()


fn = args.path
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does the variable name fn mean?

fn = args.path
listDir = os.listdir(fn)

if fn != "":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When would fn be empty? From the naming here, it's pretty hard to work out what this means.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, Thank you for your feedback @illicitonion .
Since the path argument defaults to ".", the condition would never be false, so I have removed the unnecessary check.
I’ve also renamed the variable to make it clearer that it represents a directory path.

Comment on lines 54 to 55
if len(args.path) > 1 and not args.l and not args.w and not args.c:
print(lineCounter, wordCounter, charCounter, "total") No newline at end of file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The real wc shows totals even if you specify flags, e.g. wc -c /file/1 /file/2 will show a total row.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your feedback, Daniel. I have refactored my code accordingly. please let me know if any further changes are needed. I really appreciate your suggestion.

@illicitonion illicitonion removed the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Feb 20, 2026
@aydaeslami aydaeslami added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Feb 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. Reviewed Volunteer to add when completing a review with trainee action still to take. 📅 Sprint 4 Assigned during Sprint 4 of this module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants